<div class="layuimini-container layuimini-page-anim">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="name" autocomplete="off" class="layui-input /">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button class="layui-btn layuiadmin-btn-list" lay-submit lay-filter="search-grade-submit">
                                <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>
        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加年级 </button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="deletePart"> 批量删除 </button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <!-- <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a> -->
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

    </div>
</div>
<button id="importExcel" style="display: none" ></button>
<script>
    layui.use(['form', 'table','miniPage','element'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            miniPage = layui.miniPage;

        table.render({
            elem: '#currentTableId',
            id: 'grade-table-id',//重载需要
            url: '/grade/partList',
            method: 'post',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'print'], //头部工具栏
            // cellMinWidth: 80,
            cols: [[
                {type: "checkbox", width: 50},
                {field: 'id', title: 'ID', sort: true},
                {field: 'name', title: '名称'},
                {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [5, 10, 15, 20, 25, 50, 100],
            limit: 10,
            skin: 'line',
            page: true,//开启分页
            parseData: function (res) { //res 即为原始返回的数据
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.message, //解析提示文本
                    "count": res.data.grades.total, //解析数据长度
                    "data": res.data.grades.records //解析数据列表
                };
            }
        });
        // 监听搜索操作
        form.on('submit(search-grade-submit)',function (data) {
            table.reload('grade-table-id',{
                where:{
                    "name":data.field.name
                }
            });
            return false;
        });

        /**
         * toolbar(表格头部工具栏)
         * tabel.on 事件监听
         * 监听表格的头部工具栏
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            switch (obj.event) {
                case "add":{
                    var content = miniPage.getHrefContent('grade/add.html');
                    var openWH = miniPage.getOpenWidthHeight();

                    var index = layer.open({
                        title: '添加年级',
                        type: 1,//页面层
                        shade: 0.2,
                        maxmin:false,
                        shadeClose: true,
                        area: [openWH[0] + 'px', openWH[1] + 'px'],
                        offset: [openWH[2] + 'px', openWH[3] + 'px'],
                        content: content,
                    });
                    $(window).on("resize", function () {
                        layer.full(index);
                    });
                    break;
                }
                case "deletePart":{
                    var data = table.checkStatus('grade-table-id').data
                    console.log(data)
                    if(data.length==0){
                        layer.msg("请至少选择一条数据删除")
                    }else{
                        layer.confirm('真的要删除这些年级数据吗？', function (index) {
                            let gradeIds = data.map(item=>item.id)
                            gradeIdsStr = gradeIds.join(",")
                            axios.post("grade/deletePart/"+gradeIdsStr)
                                .then(data=>{
                                    if(data.code==0){
                                      layer.msg(data.message,{
                                        iron:6,
                                        time:2000
                                      },function () {
                                          table.reload("grade-table-id")
                                      })
                                    }else{
                                      layer.msg(data.method,{
                                        iron:5,
                                        time:2000,
                                        anim:6
                                      })  
                                    }
                                }).catch(error=>{
                                    layer.msg("请求错误",{
                                      iron:5,
                                      time:2000,
                                      anim:6
                                    })
                            });
                        });
                    }
                }
            }
        });
        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {

                var content = miniPage.getHrefContent('grade/edit.html');
                var openWH = miniPage.getOpenWidthHeight();

                var index = layer.open({
                    title: '编辑年级',
                    type: 1,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: [openWH[0] + 'px', openWH[1] + 'px'],
                    offset: [openWH[2] + 'px', openWH[3] + 'px'],
                    content: content,
                });
                form.val("edit-grade", { //formTest 即 class="layui-form" 所在元素属性 lay-filter="" 对应的值
                    "id":data.id,
                    "name":data.name
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('真的要删除该年级数据吗？', function (index) {
                    axios({
                        method:'post',
                        url:'/grade/deleteOne/'+data.id
                    }).then(data=>{
                      if(data.code==0){
                        layer.msg(data.message,{
                          icon:6,
                          time:2000,
                        },function () {
                            table.reload("grade-table-id")
                        })
                      }else{
                        layer.msg(data.message,{
                          icon:5,
                          time:2000,
                          anim:6
                        })
                      }   
                    }).catch(error=>{
                        layer.msg("请求错误",{
                          icon:5,
                          time:2000,
                          anim:6
                        })
                    });
                    layer.close(index);
                });
            }
        });

    });
</script>